//
//  AppTool.swift
//  SkyWeather
//
//  Created by lx on 2020/11/12.
//

import UIKit

class AppTool {
    
    private static let sharedUserDefaults = UserDefaults.standard
    
    static func saveObject(object: Any, key: String) {
        sharedUserDefaults.set(object, forKey: key)
        sharedUserDefaults.synchronize()
    }
    
    static func getObject(key: String) -> Any? {
        return sharedUserDefaults.object(forKey: key)
    }
    
    static let isPhoneX: Bool = {
        if #available(iOS 11.0, *) {
            return (UIApplication.shared.windows.first?.safeAreaInsets.bottom ?? 0.0) > 0.0
        } else {
            return false
        }
    }()
    
    class func timeDescSinceNow(date: NSDate) -> String {
        let timeInterval = Int(date.timeIntervalSince(Date()))
        
        if timeInterval < 60 {
            return String(format: "%02ld秒", timeInterval)
        }else if timeInterval < 3600 {
            let minute = timeInterval / 60
            let second = timeInterval % 60
            return String(format: "%02ld分%02ld秒", minute, second)
        }else if timeInterval < 24 * 3600 {
            let hour = timeInterval / 3600
            let minute = (timeInterval / 60) % 60
            let second = timeInterval % 60
            return String(format: "%02ld时%02ld分%02ld秒", hour, minute, second)
        }else {
            let day = timeInterval / 3600 / 24
            let hour = (timeInterval / 3600) % 24
            let minute = (timeInterval / 60) % 60
            let second = timeInterval % 60
            return String(format: "%ld天%02ld时%02ld分%02ld秒", day, hour, minute, second)
        }

    }

    class func currentVC() -> UIViewController? {
        var result: UIViewController?
        var window = UIApplication.shared.windows.first
        if window?.windowLevel != .normal {
            if let tmpWin = UIApplication.shared.windows.first(where: { $0.windowLevel == .normal }) {
                window = tmpWin
            }
        }
        
        var nextResponder: UIViewController?
        var appRootVC = window?.rootViewController
        if appRootVC?.presentedViewController != nil {
            while (appRootVC?.presentedViewController != nil) {
                appRootVC = appRootVC?.presentedViewController
                if appRootVC != nil {
                    nextResponder = appRootVC
                }else {
                    break
                }
            }
        }else {
            nextResponder = appRootVC
        }
        
        if let tabbar = nextResponder as? UITabBarController {
            let nav = tabbar.viewControllers?[tabbar.selectedIndex] as? UINavigationController
            result = nav?.viewControllers.last
        }else if let nav = nextResponder as? UINavigationController {
            result = nav.viewControllers.last
        }else {
            result = nextResponder
        }
        return result
    }
    
    class func rootView() -> UIView? {
        return UIApplication.shared.keyWindow
    }

    class func openApplicationURL(url: URL?) {
        let application = UIApplication.shared
        if let url = url, application.canOpenURL(url) {
            application.open(url, options: [:]) { (success) in
                
            }
        }
    }

}
